package com.ustc.hewei.blogmodule.serviceImpl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ustc.hewei.commonmodule.dto.Result;
import com.ustc.hewei.commonmodule.dto.UserDTO;
import com.ustc.hewei.commonmodule.entity.Follow;
import com.ustc.hewei.commonmodule.mapper.FollowMapper;
import com.ustc.hewei.commonmodule.service.IFollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ustc.hewei.commonmodule.service.IUserService;
import com.ustc.hewei.nettyrpc2_0.common.annotation.AutowireService;
import com.ustc.hewei.nettyrpc2_0.common.annotation.RPCService;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.Collections;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import static com.ustc.hewei.blogmodule.utils.Constants.FOLLOW_KEY;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hewei
 * @since 2022-10-05
 */
@Service
@RPCService(value = IFollowService.class)
public class FollowServiceImpl extends ServiceImpl<FollowMapper, Follow> implements IFollowService {

    public IUserService getUserService() {
        return userService;
    }

    public void setUserService(IUserService userService) {
        this.userService = userService;
    }

    @Resource
    private IUserService userService;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public Result follow(Long followedUserId, boolean isFollowed, UserDTO userDTO) {
        if (userDTO == null) {
            return Result.fail("请先登录!");
        }
        Long userId = userDTO.getId();
        String key = FOLLOW_KEY + userId;
        String followedUserIdStr = followedUserId.toString();
        if (isFollowed) {
            Follow follow = new Follow();
            follow.setUserId(userId).setFollowUserId(followedUserId);
            boolean isSuccess = save(follow);
            if (isSuccess) {
                stringRedisTemplate.opsForSet().add(key, followedUserIdStr);
            }
        } else {
            boolean isSuccess = remove(new QueryWrapper<Follow>().eq("follow_user_id", followedUserId)
                    .eq("user_id", userId));
            if (isSuccess) {
                stringRedisTemplate.opsForSet().remove(key, followedUserIdStr);
            }
        }
        return Result.ok();
    }

    @Override
    public Result isFollow(Long followedUserId, UserDTO userDTO) {
        if (userDTO == null) {
            return Result.fail("请先登录!");
        }
        Long userId = userDTO.getId();
        Boolean isMember = stringRedisTemplate.opsForSet().isMember(FOLLOW_KEY + userId, followedUserId.toString());
        return Result.ok(isMember);
    }

    @Override
    public Result commonFollow(Long followedUserId, UserDTO userDTO) {
        if (userDTO == null) {
            return Result.fail("请先登录!");
        }
        Long userId = userDTO.getId();
        Set<String> ids = stringRedisTemplate.opsForSet().intersect(FOLLOW_KEY + userId, FOLLOW_KEY + followedUserId);
        if (ids == null || ids.size() == 0) {
            return Result.ok(Collections.emptyList());
        }
        List<Long> idList = ids.stream().map(Long::valueOf).collect(Collectors.toList());
        List<UserDTO> userDTOList = userService.listByIds(idList)
                .stream().map(user -> BeanUtil.copyProperties(user, UserDTO.class))
                .collect(Collectors.toList());
        return Result.ok(userDTOList);
    }
}
